
package org.itboys.mobile.service;

import org.apache.commons.lang.StringUtils;
import org.itboys.admin.entity.lasted.SystemDictionary;
import org.itboys.admin.service.lasted.SystemDictionaryService;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.product.SundayProductBrandMapper;
import org.itboys.mobile.dto.SundayAdvertisementDto;
import org.itboys.mobile.dto.product.SundayProductBrandDto;
import org.itboys.mobile.dto.product.SundayProductCategoryDto;
import org.itboys.mobile.dto.product.SundayProductDto;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.entity.mysql.product.SundayProductBrand;
import org.itboys.mobile.service.mongo.front.SundayFrontAdvertisementService;
import org.itboys.mobile.service.mongo.web.SundayMemberService;
import org.itboys.mobile.service.mysql.front.SundayFrontProductBrandService;
import org.itboys.mobile.service.mysql.front.SundayFrontProductCategoryService;
import org.itboys.mobile.service.mysql.front.SundayFrontProductService;
import org.itboys.mobile.service.mysql.front.SundayFrontWalletService;
import org.itboys.param.ParamUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.provider.Sun;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_前端首页（微信/app需要的数据）_权限。
 */

@Service
public class SundayIndexService {
    protected Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private SundayFrontAdvertisementService advertisementService;
    @Autowired
    private SundayFrontProductCategoryService productCategoryService;
    @Autowired
    private SundayFrontProductService productService;
    @Autowired
    private SystemDictionaryService dictionaryService;
    @Autowired
    private ResourceHolder resourceHolder;
    @Autowired
    private SundayFrontProductBrandService brandService;
    @Autowired
    private SundayMemberService memberService;
    @Autowired
    private SundayFrontWalletService walletService;
    /**
     * 获取首页数据。
     * @param request
     * @param memberId
     * @param type 1首页，2TOP10，10达人试用，5分销专区，8今日疯抢，4海淘（全球购），9拼团，6爱购币专区
     * @return
     */
        public Map<String,Object> getIndexData(HttpServletRequest request, Long memberId,Integer type){
            Map<String,Object> result = new HashMap<String,Object>();
            Map<String,Object> param = new HashMap<String,Object>();
            String imgRoot = resourceHolder.getStringValue("imgRoot");
            //首页
            if(type==null||type==1){
                //首页轮播图
                List<SundayAdvertisementDto> index_rolls = advertisementService.getAdvertisementByZone("index_roll");
                result.put("index_rolls",index_rolls);

                //全部一级和二级分类
                List<SundayProductCategoryDto> categories = productCategoryService.getTree(memberId,0l);
                result.put("categories",categories);
                //首页图标和图片
                List<Map<String,Object>> icons = new ArrayList<>();
                List<Map<String,Object>> images = new ArrayList<>();
                List<Map<String,Object>> buttons= new ArrayList<>();
                param.clear();
              //首页logo和图标
                param.put("isDeleted",SundayCommonConstants.NO);
                param.put("key in",new String[]{"index_icon","index_image","index_button"});
                List<SystemDictionary> index_icons_and_images=dictionaryService.list(param);
                for(SystemDictionary index_icon_and_image:index_icons_and_images){
                    Map<String,Object> icon= new HashMap<String,Object>();
                    Map<String,Object> image= new HashMap<String,Object>();
                    Map<String,Object> button= new HashMap<String,Object>();
                    if(index_icon_and_image.getKey().equalsIgnoreCase("index_icon")){
                        icon.put("name",index_icon_and_image.getName());
                        icon.put("value",index_icon_and_image.getValue());
                        icon.put("image",imgRoot+index_icon_and_image.getImage());
                        icons.add(icon);
                    }
                    if(index_icon_and_image.getKey().equalsIgnoreCase("index_image")){
                        image.put("name",index_icon_and_image.getName());
                        image.put("value",index_icon_and_image.getValue());
                        image.put("image",imgRoot+index_icon_and_image.getImage());
                        images.add(image);
                    }
                    if(index_icon_and_image.getKey().equalsIgnoreCase("index_button")){
                        button.put("name",index_icon_and_image.getName());
                        button.put("value",index_icon_and_image.getValue());
                        button.put("image",imgRoot+index_icon_and_image.getImage());
                        buttons.add(button);
                    }

                }
                result.put("icons",icons);
                result.put("images",images);
                result.put("buttons",buttons);
                param.clear();
                //推荐商品
                param.put("status", SundayCommonConstants.product_status_ysj);
                param.put("storeBiggerEqual",1);//库存大于等于
                param.put("isRecommend",SundayCommonConstants.YES);
                param.put("types", Arrays.asList(new Integer[]{
                        SundayCommonConstants.type_putong,
                        SundayCommonConstants.type_xinpin,
                        SundayCommonConstants.type_temai,
                        SundayCommonConstants.type_haitao,
                }));
                param.put("sort_name","saleNumber");
                param.put("sort_order","DESC");
               // param.put("row_start",0);
                //param.put("row_end",20);
                List<SundayProductDto> products=productService.selectProductNoPage(param);
                for(SundayProductDto productDto:products){
                    if(StringUtils.isNotEmpty(productDto.getName())&&
                            productDto.getName().length()>6){
                        productDto.setName(productDto.getName().substring(0,7)+"...");
                    }
                }
                result.put("products",products);
           //2,top10
            }else if(type==2){
                param.put("status", SundayCommonConstants.product_status_ysj);
                param.put("storeBiggerEqual",1);//库存大于等于
                param.put("types",Arrays.asList(new Integer[]{
                        SundayCommonConstants.type_putong,
                        SundayCommonConstants.type_xinpin,
                        SundayCommonConstants.type_temai,
                }));
                param.put("sort_name","saleNumber");
                param.put("sort_order","DESC");
                param.put("row_start",0);
                param.put("row_size",10);
                List<SundayProductDto> products=productService.selectProductNoPage(param);
                result.put("products",products);
            //达人试用
            }else if(type==10){

            //分销专区
            }else if(type==5){
                result.put("info","分销商品海淘请使用商品接口分页获取");
            //全球购，
            }else if(type==4) {
                List<SundayProductBrandDto> brands = brandService.getBrands(0l,0l,null,SundayCommonConstants.YES);
                result.put("brands",brands);
                result.put("info","海淘商品请使用商品接口分页获取");
            //今日疯抢（秒杀）,取出秒杀商品不分页，然后再匹配对应的秒杀时间段
            }else if(type==8){
                List<SystemDictionary> secKillTimeGroups =dictionaryService.getDictionaryByKey("secKillTimeGroup");
                List<Long> secKillTimeGroupIds = new ArrayList<>();
                for(SystemDictionary secKillTimeGroup:secKillTimeGroups){
                    secKillTimeGroupIds.add(secKillTimeGroup.getId());
                }
                param.clear();
                param.put("status",SundayCommonConstants.product_status_ysj);
                param.put("type",SundayCommonConstants.type_miaosha);
                param.put("secKillTimeGroupIds",secKillTimeGroupIds);
                List<SundayProductDto> productDtos = productService.selectProductNoPage(param);
                //按时间组匹配对应商品
                Integer nowHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
                 List<Map<String,Object>> timeGroups = new ArrayList<>();

                //2018年2月3日，默认选择最近秒杀组
                boolean isCheckBool=false;
                for(SystemDictionary secKillTimeGroup:secKillTimeGroups){
                    Integer isCheck=0;
                    Map<String,Object> timeGroup = new HashMap<String,Object>();
                    timeGroup.put("name",secKillTimeGroup.getName());
                    Integer status=0;
                    String statusStr ="";

                     if(Integer.valueOf(secKillTimeGroup.getValue())>nowHour){
                        status=2;
                        statusStr="即将开抢";
                         //默认选择最近秒杀组
                         if(!isCheckBool){
                             isCheck=1;
                             isCheckBool=true;
                         }
                    }else if(Integer.valueOf(secKillTimeGroup.getValue())<nowHour) {
                        status = 3;
                         statusStr="已结束";
                    }else{
                        status=1;
                        statusStr="抢购中";
                        //默认选择最近秒杀组
                         if(!isCheckBool){
                             isCheck=1;
                             isCheckBool=true;
                         }
                    }

                    timeGroup.put("status",status);
                    timeGroup.put("statusStr",statusStr);
                    timeGroup.put("isCheck",isCheck);
                    //匹配商品
                    List<SundayProductDto> products = new ArrayList<>();
                    for(SundayProductDto productDto:productDtos){
                        if(secKillTimeGroup.getValue().equalsIgnoreCase(productDto.getSecKillTimeGroupValue())){
                            products.add(productDto);
                        }
                    }
                    timeGroup.put("products",products);
                    timeGroups.add(timeGroup);
                }
                result.put("timeGroups",timeGroups);

            //拼团
            }else if(type==9){
                //获取拼团商品
                result.put("info","拼团商品请使用商品接口分页获取");
            //爱购币专区
            }else if(type==6){
                //2018年2月24日，返回返回当前可用爱购币
                result.put("aigouCoin",walletService.getByMemberId(memberId).getAigouCoin());
                result.put("info","爱购币商品请使用商品接口分页获取");
            }
            return result;
        }

    /**
     * 获取底部数据
     * @return
     */
    public Map<String,Object> getBottomData(){
        Map<String,Object> result = new HashMap<String,Object>();
        Map<String,Object> param = new HashMap<String,Object>();
        String imgRoot = resourceHolder.getStringValue("imgRoot");
        List<Map<String,Object>> buttons = new ArrayList<>();
        param.clear();
        //首页logo和图标
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("key","index_button");
        List<SystemDictionary> index_icons_and_images=dictionaryService.list(param);
        for(SystemDictionary index_icon_and_image:index_icons_and_images){
            Map<String,Object> button= new HashMap<String,Object>();
           // if(index_icon_and_image.getKey().equalsIgnoreCase("index_button")){
                button.put("name",index_icon_and_image.getName());
                button.put("value",index_icon_and_image.getValue());
                button.put("image",imgRoot+index_icon_and_image.getImage());
                buttons.add(button);
          //  }
        }
        result.put("buttons",buttons);
     //   result.put("type",type);

        return result;
    }

}